<?php

class NewsApp extends MemberbaseApp
{
	var $_news_mod;
    function __construct()
    {
        $this->NewsApp();
    }
    function NewsApp()
    {
        parent::__construct();
		$user = $this->visitor->get();
		$user_mod =& m('member');
		if (!$user_mod->check_powers($user['user_id'], array(USER_ADMIN, USER_INFO))) {
			$this->show_warning('no_powers_to_visit');
			exit;
		};
		$this->_news_mod =& m('news');
    }
	
    function index()
    {
		$brands = $this->_brand_mod->findAll();
		$this->assign('brands', $brands);
        /* 当前位置 */
        $this->_curlocal(LANG::get('brands'));

        /* 当前用户中心菜单 */
        $this->_curitem('brands');
        $this->_config_seo('title', Lang::get('brands'));
        $this->display('brand.index.html');
    }
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('brands'), 'index.php?app=brands',
				Lang::get('add_new_brand'));

        	/* 当前用户中心菜单 */
	        $this->_curitem('brands');
	        $this->_config_seo('title', Lang::get('brands') . ' - ' . Lang::get('add_new_brand'));
	        $this->display('brand.form.html');
		} else {
			$brand_name = trim($_POST['brand_name']);
			$description = trim($_POST['description']);
			$if_show = $_POST['if_show'];
			
			if (empty($brand_name)) {
				$this->show_warning('brand_name_not_empty');
				return;
			}
			if (!$this->_brand_mod->unique($brand_name)) {
				$this->show_warning('brand_name_is_exist');
				return;
			}
			
			$data = array(
				'brand_name' => $brand_name,
				'description' => $description,
				'if_show' => $if_show
			);
			
			$brand_id = $this->_brand_mod->add($data);
			if (!$brand_id) {
				$this->_error($this->_brand_mod->get_error());
				return;
			}
			$this->show_message('add_brand_success', Lang::get('brands'), 'index.php?app=brands');
		}
	}
	
	function _assign_form()
	{
		$brand = array(
			'if_show' => 1,
		);
		$this->assign('brand', $brand);
	}
	
	function edit()
	{
		$brand_id = intval($_GET['brand_id']);
		if (!$brand_id) {
			$this->show_warning('brand_not_exit');
		}
		if (!IS_POST) {
			$brand = $this->_brand_mod->get_info($brand_id);
			$this->assign('brand', $brand);
			
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('brands'), 'index.php?app=brands',
				Lang::get('edit_brand'));

        	/* 当前用户中心菜单 */
	        $this->_curitem('brands');
	        $this->_config_seo('title', Lang::get('brands') . ' - ' . Lang::get('edit_brand'));
	        $this->display('brand.form.html');
		} else {
			$brand_name = trim($_POST['brand_name']);
			$description = trim($_POST['description']);
			$if_show = $_POST['if_show'];
			
			if (empty($brand_name)) {
				$this->show_warning('brand_name_not_empty');
				return;
			}
			if (!$this->_brand_mod->unique($brand_name, $brand_id)) {
				$this->show_warning('brand_name_is_exist');
				return;
			}
			
			$data = array(
				'brand_name' => $brand_name,
				'description' => $description,
				'if_show' => $if_show
			);
			$this->_brand_mod->edit($brand_id, $data);
			if ($this->_brand_mod->get_error()) {
				$this->_error($this->_brand_mod->get_error());
				return;
			}
			$this->show_message('edit_brand_success', Lang::get('brands'), 'index.php?app=brands');
		}
	}
}

?>
